<template>
	<view>
		<view class="search-box">
			<uni-search-bar @confirm="search" @input="input" :radius="100" bgColor="#fff" cancelButton='none' ></uni-search-bar>
		</view>
		<view class="history-box" v-if="searchResults.length===0">
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type='trash' size="17" @click="clean"></uni-icons>
			</view>
			<view class="history-list">
				<uni-tag type="default" v-for="(v,i) in histories" :key="i" :text="v" @click='gotoGoodList(v)' custom-style="background-color:#fff;margin:0 16rpx;color:#000"></uni-tag>
			</view>
		</view>
		<view class="sugg-list" v-else>
			<view class="sugg-item" v-for="(v,i) in searchResults" :key="i" @click='gotoDetails(v.goods_id)' >
				<view class="goods-name">
					{{v.goods_name}}
				</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer:null,
				kw:'',
				searchResults:[],
				//搜搜历史
				historyList:[],
				
			};
		},
		methods:{
			input(e){
				clearTimeout(this.timer)
				this.timer=setTimeout(()=>{
					this.kw=e;
					this.getSearchList()
				},500)
			},
			getSearchList(){
				if(this.kw.length===0){
					this.searchResults=[];
					return
				}
				uni.request({
					url:'https://api-hmugo-web.itheima.net/api/public/v1/goods/qsearch',
					method:"GET",
					data:{query:this.kw},
				}).then(res=>{
					// console.log(res.data)
					this.searchResults=res.data.message
				})
				this.saveSearchHistory()
			},
			saveSearchHistory(){
				// if(!this.historyList.some(v=> v===this.kw))
				// this.historyList.unshift(this.kw)
				const set=new Set(this.historyList)
				set.delete(this.kw)
				set.add(this.kw)
				this.historyList=Array.from(set)
				uni.setStorageSync('kw',JSON.stringify(this.historyList))
			},
			clean(){
				this.historyList=[];
				uni.removeStorageSync('kw')
			},
			gotoDetails(id){
				uni.navigateTo({
					url:'/subpkg/good_detail/good_detail?goods_id='+id
				})
			},
			gotoGoodList(item){
				uni.navigateTo({
					url:'/subpkg/good_list/good_list?query='+item
				})
			}
		},
		computed:{
			histories(){
				return [...this.historyList].reverse()
			}
		},
		onLoad() {
			this.historyList=JSON.parse(uni.getStorageSync('kw')|| '[]')
		}
	}
</script>

<style lang="scss">
.search-box{
	position: sticky;
	top: 0;
	z-index: 999;
	background-color: #c00000;
}
.history-box{
	padding: 0 10rpx;
	.history-title{
		padding: 12rpx 0;
		display: flex;
		justify-content: space-between;
		font-size: 30rpx;
		font-weight: bold;
		border-bottom: 2rpx solid #efefef;
	}
	.history-list{
		display: flex;
		flex-wrap: wrap;
	}
}
.sugg-list{
	padding: 0 10rpx;
	.sugg-item{
		font-size: 24rpx;
		padding: 26rpx 0;
		border-bottom: 2rpx solid #efefef;
		display: flex;
		justify-content: space-between;
		align-items: center;
		.goods-name{
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
			margin-right: 6rpx;
		}
	}
}
</style>
